package com.bookstore.controller;


import com.alibaba.fastjson.JSON;
import com.bookstore.entity.*;
import com.bookstore.service.BookCarService;
import com.bookstore.service.UserMessageService;
import com.bookstore.service.impl.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author :Cofe
 * @Date 2022/4/21 21:48
 */
@Controller
@RequestMapping("/bookcar")
public class BookCarContorller {
    @Resource
    private BookServiceImpl bookService;
    @Resource
    private BookCarServiceImpl bookCarService;
    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private UserMessageServiceImpl userMessageService;

    @RequestMapping(
            value = "/addBookCar",
            method = RequestMethod.POST,
            produces = {"app/xml;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String insertBook(int bid, String book_name, double book_balance,String book_pic,double book_membalance,HttpSession httpSession) {
        if(httpSession.getAttribute("user_uid")==null){
            return "请您先登陆";
        }
        Object user_uid1 = httpSession.getAttribute("user_uid");
        int user_uid=Integer.parseInt(user_uid1+"");
        bookCarService.addBookCar(bid,book_name, book_balance,user_uid,book_pic,book_membalance);
        return "添加到购物车成功";
    }
    @RequestMapping(
            value = "/findallbookcar",
            method = RequestMethod.POST
    )
    @CrossOrigin
    @ResponseBody
    public List<BookCar> findbookcar(HttpSession httpSession) {
        Object user_uid1 = httpSession.getAttribute("user_uid");
        int user_uid=Integer.valueOf(user_uid1+"");
        List<BookCar> list=bookCarService.findcarbyid(user_uid);
        return list;
    }
    @RequestMapping(
            value = "/deleteAllinfo",
            method = RequestMethod.POST,
            produces = {"app/xml;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String deleteAllinfo(HttpSession httpSession) {
        Object user_uid1 = httpSession.getAttribute("user_uid");
        String username = httpSession.getAttribute("username")+"";
        int user_uid=Integer.valueOf(user_uid1+"");
        List<BookCar> list=bookCarService.findcarbyid(user_uid);
        UserMessage um= userMessageService.findByUsername(username);
        for (BookCar bookCar : list) {
            orderService.insertInto(bookCar.getBid(),bookCar.getBook_name(),bookCar.getBook_balance(),bookCar.getUser_uid(),bookCar.getBook_pic(),
                    bookCar.getBuy_id(),bookCar.getBook_membalance(),username,um.getAddress(),um.getPhone());
            Book book=bookService.findBookById(bookCar.getBid());
            int book_sale = book.getBook_sale();
            int book_number = book.getBook_number();
            System.out.println(book_number);
            if(book_number==0){
                return "库存不够,请等待补货...";
            }
            book.setBook_sale(book_sale+1);         //购买成功销量+1
            book.setBook_number(book_number-1);     //购买成功库存-1
            bookService.updateBookNew(book.getBid(),book.getBook_number(),book.getBook_sale());
        }
        bookCarService.deleteAll(user_uid);             //支付成功加入订单
        return "success";
    }

    @RequestMapping(
            value = "/deleteinfo",
            method = RequestMethod.POST,
            produces = {"app/xml;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    //删除购物车中的不想购买的图书
    public String deleteinfo(String buy_id) {
        boolean b = bookCarService.deleteByBuyId(Integer.parseInt(buy_id));
        if(b){
            return "success";
        }else{
            return "error";
        }
    }

    @RequestMapping(
            value = "/buybook",
            method = RequestMethod.POST,
            produces = {"app/xml;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    //购买单个图书
    public String buybook(String buy_id,HttpSession session) {
        BookCar bookCar = bookCarService.findBybuy_id(Integer.parseInt(buy_id));
        if(bookCar!=null){
            Book book= bookService.findBookById(bookCar.getBid());
            int book_sale = book.getBook_sale();
            int book_number = book.getBook_number();
            if(book_number==0){
                return "库存不够,请等待补货...";
            }
            UserMessage um= userMessageService.findByUsername(session.getAttribute("username") + "");
            System.out.println(um.getAddress());
            if(um.getAddress().equals("") || um.getPhone().equals("")){
                return "请完善所有信息!!!";
            }
            boolean b = bookCarService.deleteByBuyId(Integer.parseInt(buy_id));
            if(b){
                book.setBook_sale(book_sale+1);         //购买成功销量+1
                book.setBook_number(book_number-1);     //购买成功库存-1
                String username=session.getAttribute("username")+"";
                orderService.insertInto(bookCar.getBid(),bookCar.getBook_name(),bookCar.getBook_balance(),bookCar.getUser_uid(),
                        bookCar.getBook_pic(),bookCar.getBuy_id(),bookCar.getBook_membalance(),username,
                        um.getAddress(),um.getPhone());
                bookService.updateBookNew(book.getBid(),book.getBook_number(),book.getBook_sale());
                return "success";
            }
            else{
                return "系统错误,请稍后重试...";
            }
        }else{
            return "该图书已经下架...";
        }
    }

    @RequestMapping(
            value = "/selectbypage",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String selectByPage(int currentPage,int pageSize,HttpSession httpSession) {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        Object user_uid1 = httpSession.getAttribute("user_uid");
        int user_uid=Integer.valueOf(user_uid1+"");
        //2. 调用service查询
        PageBean<BookCar> pageBean = bookCarService.selectByPage(currentPage, pageSize,user_uid);
        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        return jsonString;
    }
}
